{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "52098d0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4acf0566",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[-0.6667, -0.8000],\n",
      "         [-0.6667, -0.4000],\n",
      "         [-0.6667,  0.0000],\n",
      "         [-0.6667,  0.4000],\n",
      "         [-0.6667,  0.8000]],\n",
      "\n",
      "        [[ 0.0000, -0.8000],\n",
      "         [ 0.0000, -0.4000],\n",
      "         [ 0.0000,  0.0000],\n",
      "         [ 0.0000,  0.4000],\n",
      "         [ 0.0000,  0.8000]],\n",
      "\n",
      "        [[ 0.6667, -0.8000],\n",
      "         [ 0.6667, -0.4000],\n",
      "         [ 0.6667,  0.0000],\n",
      "         [ 0.6667,  0.4000],\n",
      "         [ 0.6667,  0.8000]]])\n",
      "torch.Size([3, 5, 2])\n",
      "tensor([[[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]],\n",
      "\n",
      "\n",
      "        [[[-0.6667, -0.8000],\n",
      "          [-0.6667, -0.4000],\n",
      "          [-0.6667,  0.0000],\n",
      "          [-0.6667,  0.4000],\n",
      "          [-0.6667,  0.8000]],\n",
      "\n",
      "         [[ 0.0000, -0.8000],\n",
      "          [ 0.0000, -0.4000],\n",
      "          [ 0.0000,  0.0000],\n",
      "          [ 0.0000,  0.4000],\n",
      "          [ 0.0000,  0.8000]],\n",
      "\n",
      "         [[ 0.6667, -0.8000],\n",
      "          [ 0.6667, -0.4000],\n",
      "          [ 0.6667,  0.0000],\n",
      "          [ 0.6667,  0.4000],\n",
      "          [ 0.6667,  0.8000]]]])\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Anaconda3\\lib\\site-packages\\torch\\functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  ..\\aten\\src\\ATen\\native\\TensorShape.cpp:2157.)\n",
      "  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]\n"
     ]
    }
   ],
   "source": [
    "H_key, W_key = 3, 5\n",
    "ref_y, ref_x = torch.meshgrid(\n",
    "    torch.linspace(0.5, H_key - 0.5, H_key),\n",
    "    torch.linspace(0.5, W_key - 0.5, W_key)\n",
    ")\n",
    "ref = torch.stack((ref_y, ref_x), -1)\n",
    "ref[..., 1].div_(W_key).mul_(2).sub_(1)\n",
    "ref[..., 0].div_(H_key).mul_(2).sub_(1)\n",
    "print(ref)\n",
    "print(ref.shape)\n",
    "ref = ref[None, ...].expand(10, -1, -1, -1)  # B * g H W 2\n",
    "print(ref)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "84566d06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "31287464",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 6, 4])\n",
      "torch.Size([1, 2, 3, 4])\n",
      "torch.Size([1, 4, 2, 3])\n"
     ]
    }
   ],
   "source": [
    "test = torch.randn(1, 2*3, 4)\n",
    "print(test.shape)\n",
    "test = test.view(1, 2, 3, 4)\n",
    "print(test.shape)\n",
    "test = test.permute(0, 3, 1, 2).contiguous()\n",
    "print(test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7ca0323b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 4, 2, 3])\n"
     ]
    }
   ],
   "source": [
    "test2 = torch.randn(1, 2*3, 4)\n",
    "test3 = test2.reshape(1, 2, 3, 4).permute(0, 3, 1, 2).contiguous()\n",
    "print(test3.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77206a6f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
